<!-- 编辑弹窗 -->
<template>
  <ele-modal :form="true" :destroy-on-close="true" :width="460" v-model="visible"
    :title="isUpdate ? '修改导出记录管理' : '添加导出记录管理'">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" @submit.prevent="">
      <el-form-item label="文件类型" prop="fileType">
        <el-input clearable v-model="form.fileType" placeholder="请输入文件类型" />
      </el-form-item>
      <el-form-item label="文件名称" prop="fileName">
        <el-input clearable v-model="form.fileName" placeholder="请输入文件名称" />
      </el-form-item>
      <el-form-item label="状态" prop="fileStatus">
        <el-input clearable v-model="form.fileStatus" placeholder="请输入状态" />
      </el-form-item>
      <el-form-item label="失败原因" prop="failureReason">
        <el-input :rows="4" type="textarea" v-model="form.failureReason" placeholder="请输入失败原因" />
      </el-form-item>
      <el-form-item label="文件存储路径" prop="filePath">
        <el-input :rows="4" type="textarea" v-model="form.filePath" placeholder="请输入文件存储路径" />
      </el-form-item>
      <el-form-item label="文件大小(字节)" prop="fileSize">
        <el-input clearable v-model="form.fileSize" placeholder="请输入文件大小(字节)" />
      </el-form-item>
      <el-form-item label="下载次数" prop="downloadCount">
        <el-input clearable v-model="form.downloadCount" placeholder="请输入下载次数" />
      </el-form-item>
      <el-form-item label="最近下载时间" prop="lastDownloadTime">
        <el-date-picker v-model="form.lastDownloadTime" value-format="YYYY-MM-DD" placeholder="请选择最近下载时间"
          class="ele-fluid" />
      </el-form-item>
      <el-form-item label="创建人" prop="createdBy">
        <el-input clearable v-model="form.createdBy" placeholder="请输入创建人" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker v-model="form.createdTime" value-format="YYYY-MM-DD" placeholder="请选择创建时间" class="ele-fluid" />
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy">
        <el-input clearable v-model="form.updateBy" placeholder="请输入修改人" />
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-date-picker v-model="form.updateTime" value-format="YYYY-MM-DD" placeholder="请选择修改时间" class="ele-fluid" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { EleMessage } from 'ele-admin-plus';
import { useFormData } from '@/utils/use-form-data';
import { addEtpFileManagement, updateEtpFileManagement } from '@/api/etp/etpFileManagement';

const props = defineProps({
  /** 修改回显的数据 */
  data: Object
});

const emit = defineEmits(['done']);

/** 弹窗是否打开 */
const visible = defineModel({ type: Boolean });

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  id: void 0,
  fileType: '',
  fileName: '',
  fileStatus: void 0,
  failureReason: '',
  filePath: '',
  fileSize: void 0,
  downloadCount: void 0,
  lastDownloadTime: '',
  createdBy: '',
  createdTime: '',
  updateBy: '',
  updateTime: ''
});

/** 表单验证规则 */
const rules = reactive({
  fileType: [
    {
      required: true,
      message: '请输入文件类型',
      type: 'string',
      trigger: 'change'
    }
  ],
  fileName: [
    {
      required: true,
      message: '请输入文件名称',
      type: 'string',
      trigger: 'blur'
    }
  ]
});

/** 关闭弹窗 */
const handleCancel = () => {
  visible.value = false;
};

/** 保存编辑 */
const save = () => {
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updateEtpFileManagement : addEtpFileManagement;
    saveOrUpdate(form)
      .then((msg) => {
        loading.value = false;
        EleMessage.success({ message: msg, plain: true });
        handleCancel();
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error({ message: e.message, plain: true });
      });
  });
};

/** 监听弹窗打开 */
watch(visible, () => {
  if (visible.value) {
    if (props.data) {
      assignFields(props.data);
      isUpdate.value = true;
    } else {
      resetFields();
      isUpdate.value = false;
    }
  }
});
</script>
